package ${modelPackage};

import org.apache.commons.collections4.CollectionUtils;

import java.util.*;

/**
* @author codefalse-mybatisGen-maven-plugin
*/
public class WhereFactory {
    private static final String LIKE_LEFT = "left";
    private static final String LIKE_RIGHT = "right";
    private static final String LIKE_AROUND = "around";

    private WhereFactory() {}


    public static Builder builder() {
        return new Builder();
    }

    public static class Builder{

        private final List<WhereItem> wheres;
        private final Set<String> sorts;
        private Integer limit;

        private Builder() {
            wheres = new ArrayList<>();
            sorts = new HashSet<>();
        }

        public WhereDynamic build() {
            return new WhereDynamic(wheres, sorts, limit);
        }

        private Builder addWhere(String column, String operator, Object value, String likeType) {
            if (value == null || "".equals(value)) {
                return this;
            }

            if (Objects.equals(likeType, LIKE_AROUND)) {
                value = "%" + value + "%";
            } else if (Objects.equals(likeType, LIKE_LEFT)) {
                value = "%" + value;
            } else if (Objects.equals(likeType, LIKE_RIGHT)) {
                value = value + "%";
            }

            wheres.add(new WhereItem(column, operator, value));
            return this;
        }

        private Builder addWhere(String column, String operator, Object value) {
            return addWhere(column, operator, value, null);
        }

        private Builder addLike(String column, Object value, String type) {
            return addWhere(column, "like", value, type);
        }

        public Builder in(String column, List<?> list) {
            if (CollectionUtils.isEmpty(list)) {
                return this;
            }
            return addWhere(column, "in", list);
        }

        public Builder notIn(String column, List<?> list) {
            if (CollectionUtils.isEmpty(list)) {
                return this;
            }
            return addWhere(column, "not in", list);
        }

        public Builder eq(String column, Object value) {
            return addWhere(column, "=", value);
        }

        public Builder gt(String column, Object value) {
            return addWhere(column, ">", value);
        }

        public Builder gte(String column, Object value) {
            return addWhere(column, ">=", value);
        }

        public Builder lt(String column, Object value) {
            return addWhere(column, "<", value);
        }

        public Builder lte(String column, Object value) {
            return addWhere(column, "<=", value);
        }

        public Builder leftLike(String column, Object value) {
            return addLike(column, value, LIKE_LEFT);
        }

        public Builder rightLike(String column, Object value) {
            return addLike(column, value, LIKE_RIGHT);
        }

        public Builder like(String column, Object value) {
            return addLike(column, value, LIKE_AROUND);
        }

        public Builder limit(int limit) {
            if (limit > 0) {
                this.limit = limit;
            }
            return this;
        }

        public Builder sort(String... sorts) {
            if (sorts != null && sorts.length > 0) {
                this.sorts.addAll(Arrays.asList(sorts));
            }
            return this;
        }
    }
}